<?php

namespace app\common\model;
class Wxserve
{
        // protected  $appid = "wxa2a0734b14927ebf";
        // protected  $appsecret = "a0ccda0bff7a7c02ce0095451b31dfed";
        
        protected  $appid = "wx3348d2517d9604f4";
        protected  $appsecret = "c287abef32f28c7ca93f01066f099055";
        
        
        
        /**
        
        到账金额
{{amount1.DATA}}
到账时间
{{time2.DATA}}
提现方式
{{thing3.DATA}}
        */
        //到账通知
         public function dz_money_auth($openid,$data){ 
            $templateId = 'iVa4A_TY34PYNONOaF7_aO3LEQjatb6sg5TNqTMgomQ';
       
            $data = array(
                'amount1' => array('value' =>$data["money"]),
                'time2' => array('value' =>date("Y-m-d H:i")),
                'thing3' => array('value' =>$data["pay_type"]),
            );
        
            return $this->gz_type($openid,$templateId,$data);
         }
         
        //排单超时
         public function err_z_auth($openid,$data){ 
            $templateId = 'PzrRvEAGLoXhy7DzBPE9spkaKPfGq_8k9uVQo2wBukU';
            $data = array(
                'thing1' => array('value' =>"审核不通过"),
                'thing2' => array('value' =>$data['cont']),
                'time3' => array('value' =>date("Y-m-d H:i")),
            );
        
            return $this->gz_type($openid,$templateId,$data);
         }
        
         //排单超时
         public function ok_z_auth($openid,$data){ 
            $templateId = 'M8f7qhQaGcvoh75sgI5dIFT2yIk5iHzsEZOPe6OT720';
            $data = array(
                'thing1' => array('value' =>"审核通过"),
                'time2' => array('value' =>date("Y-m-d H:i")),
            );
        
            return $this->gz_type($openid,$templateId,$data);
         }
         
         
   
        
        //排单超时
         public function cs_task_msg($openid,$data){ 
            $templateId = 'ndb_vpKgPW-sz_lCuea6XZ7VGyyjDfFD85msd_IkGNI';
            $data = array(
                'time2' => array('value' =>date("Y-m-d H:i",$data["create_time"])),
                'thing3' => array('value' =>$data["c_desc"]),
                'thing4' => array('value' =>$data["c_title"]),
                'amount6' => array('value' =>$data["money"])
            );
        
            return $this->gz_type($openid,$templateId,$data);
         }
         
        //抢单失败
         public function err_task_msg($openid,$data){ 
            $templateId = 'J3t6tOGLjumm7yRtZFIPYGOACH5M76-IgIOnuumIth4';
            $data = array(
                'character_string2' => array('value' => $data["orderNo"]),
                'phrase3' => array('value' =>"匹配失败")
            );
            return $this->gz_type($openid,$templateId,$data);
         }
        
         //抢单成功
         public function ok_task_msg($openid,$data){ 
            $templateId = 'nDTGlYbTxSzyiVW82O9tiwgFyRzr0_M5JkkEfc91XYw';
            $data = array(
                'character_string2' => array('value' => $data["orderNo"]),
                'phrase3' => array('value' =>"匹配成功"),
            );
            return $this->gz_type($openid,$templateId,$data);
         }
         
        
        //完成订单呢提醒
         public function wc_order_msg($openid,$data){ 
            $templateId = '1GZqGxMCVMDS5Dyp9cpgJ5r5fw5pZJD1kp7ItKDpROQ';
            $data = array(
                'character_string1' => array('value' => $data["orderNo"]),
                'amount2' => array('value' =>$data["price"]),
                'time4' => array('value' =>date("Y-m-d H:i")),
                'thing5' => array('value' =>$data["c_title"]),
            );
            return $this->gz_type($openid,$templateId,$data);
         }
        
         //取消订单呢提醒
         public function qx_order_msg($openid,$data){ 
            $templateId = 'hIqN_9zKPEpwYiLgoTZs4Rnnze8cL3v8-CFCQc66UK8';
            $data = array(
                'character_string1' => array('value' => $data["orderNo"]),
                'amount3' => array('value' =>$data["price"]),
                'time4' => array('value' =>date("Y-m-d H:i",$data["pay_time"])),
                'amount7' => array('value' =>$data["price"]-$data["cv_price"]),
            );
            return $this->gz_type($openid,$templateId,$data);
         }
        
        //接单提醒
         public function jd_order_msg($openid,$data){ 
 
            $templateId = '67smX7G0SYcKdXOEF2IkwmKdbjUXycoQ4T8ojupcBPw';
            $data = array(
                'character_string1' => array('value' => $data["orderNo"]),
                'time2' => array('value' => date("Y-m-d H:i")),
                'thing4' => array('value' =>$data["c_title"])
            );
            return $this->gz_type($openid,$templateId,$data);
         }
        
        //新订单提醒
         public function new_order_msg($openid,$data){ 
 
            $templateId = 'DS3xHV_nco2jRjv2f_Fj6QLRokL2oQE-ImkcenTUUEQ';
            $data = array(
                'character_string2' => array('value' => $data["orderNo"]),
                'thing6' => array('value' => $data["c_title"]),
                'thing10' => array('value' => $data["uid"]),
                'number8' => array('value' => $data["num"]),
                'amount12' => array('value' => $data["price"]),
            );
            return $this->gz_type($openid,$templateId,$data);
         }
     
     
     public function gz_type($openid,$templateId,$data){
         /*
         "miniprogram":{
             "appid":"xiaochengxuappid12345",
             "pagepath":"index?foo=bar"
           },
         **/
            $send = array(
                "touser" => $openid,
                "template_id" => $templateId,
                "data" => $data,
                "miniprogram"=>[
                    "appid"=>"wxa2a0734b14927ebf",
                    "pagepath"=>"pages/index/index",
                ]
                
            );
            $access_token = $this->getaccess_tonken();
            $data = urldecode(json_encode($send));
            $url= "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
            $admin = $this->curl_get($url,$type = 'json',$data);
            return $admin;
     }
     
        public function wx_msg($openid,$title,$desc){ 
            $templateId = 'rw8um2woPZuyWmlZFrRYLslXmrIHd1MmcUTry7Ci3Yw';
            $data = array(
                'thing1' => array('value' => $title),
                'time3' => array('value' => date('Y-m-d H:i:s', time())),
                'thing4' => array('value' => $desc),
            );
            $send = array(
                "touser" => $openid,
                "template_id" => $templateId,
                "data" => $data,
            );
            $access_token = $this->getaccess_tonken();
           
            $data = urldecode(json_encode($send));
            $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" . $access_token;
            // $ret = Http::post($url, );
            $admin = $this->curl_get($url,$type = 'json',$data);
            return $admin;
     }
    
    protected function getaccess_tonken()
   { 
        $appid = $this->appid;
        $appsecret = $this->appsecret;
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
        //api接口
        $content = file_get_contents($url);
        
        if ($content) {
            $content = json_decode($content, true);
        }
      
        return $content['access_token'];
  }
  
    protected function curl_get(string $url = '', $type = 'json', $data = array(), $header = array(), int &$httpCode = 0)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        //不做证书效验，部署在Linux环境下请改为true
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

        if ($data) {
            // POST数据
            curl_setopt($ch, CURLOPT_POST, 1);
            // 把post的变量加上
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }

        if ($header) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }

        $file_contents = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($type == 'json') {
            $res = json_decode($file_contents, TRUE);
        } else {
            $res = $file_contents;
        }

        return $res;
    }
    
}
